package com.allqj.sfx.ddd.member.domain.service;

import com.allqj.sfx.ddd.member.domain.entity.admin.AdminUser;
import com.allqj.sfx.ddd.member.domain.entity.admin.Tenant;
import com.allqj.sfx.ddd.member.domain.enums.AccountStatusEnum;
import com.allqj.sfx.ddd.member.domain.enums.BusinessExceptionEnum;
import com.allqj.sfx.ddd.member.domain.repository.AdminUserRepository;
import com.allqj.sfx.ddd.member.domain.repository.TenantRepository;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author sjw
 * @className AdminService
 * @date 2022/7/20 14:49
 * @description: 会员管理-管理员
 */
@Component
public class AdminUserService {
    private final AdminUserRepository adminUserRepository;
    private final TenantRepository tenantRepository;

    public AdminUserService(AdminUserRepository adminUserRepository,
                            TenantRepository tenantRepository) {
        this.adminUserRepository = adminUserRepository;
        this.tenantRepository = tenantRepository;
    }

    /**
     * 登录
     *
     * @param phone    电话
     * @param password 密码
     * @return
     */
    public AdminUser login(String phone, String password) {
        AdminUser adminUser = adminUserRepository.queryByPhone(phone);
        if (adminUser == null) BusinessExceptionEnum.ACCOUNT_NOT_EXIST.throwException();
        if (!password.equals(adminUser.getPassword()))
            BusinessExceptionEnum.ACCOUNT_PASSWORD_ERROR.throwException();
        if (!AccountStatusEnum.NORMAL.getCode().equals(adminUser.getStatus()))
            BusinessExceptionEnum.ACCOUNT_STATUS_ERROR.throwException();
        return adminUser;
    }

    public List<Tenant> agency() {
        return tenantRepository.tenants();
    }
}
